Node.js - require ( )


Posted by mijouhsieh on 2023-04-09

對 Node.js 來說:

  • 如果沒有提供檔案路徑, Node.js 讀取它時就會預設是模組。
  • 如果有包括路徑,那 Node.js 就知道這是個檔案。

  • 而對於副檔名,Node.js 是這樣去判斷的:
    不寫副檔名的時候,預設 require 會先把它當成 .js 處理
    如果不是 .js 則會當作 .json 處理
    如果不是 .json,就會當作 .node 處理
    如果都沒辦法讀取,程式就會炸掉

// require packages and file used in the project
const exphbs = require('express-handlebars') //模組
const movieList = require('./movies.json') //檔案

const movieList = require('./movies') //也可簡化,最好寫完整
關於 require 模組與檔案的語法,可以參考 Node 的官方文件


#node.js #require( )







Related Posts

12. Flyweight

12. Flyweight

VS code 段落過長怎麼辦?

VS code 段落過長怎麼辦?

Day04  增加資料(上) + RecycleView  Adapter

Day04 增加資料(上) + RecycleView Adapter


Comments